﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using VisionCloud.Domain.ModelAttribute;

namespace VisionCloud.Domain.Common
{
    public class ApiPageResult<T> where T : class
    {
        /// <summary>
        /// 总记录数
        /// </summary>
        public long Total { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage { get; set; }

        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage { get; set; }

        /// <summary>
        /// 当前页数据
        /// </summary>
        public List<T> PageData { get; set; }
    }

    public class PageBase
    {
        public PageBase()
        {
            PageSize = 20;
        }

        private int _pageSize;

        /// <summary>
        /// 每页显示多少条
        /// </summary>
        public int PageSize
        {
            get => _pageSize;
            set { _pageSize = value <= 0 ? int.MaxValue : value; }
        }

        public object Data { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public long Total { get; set; }

        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage { get; set; } = 1;

        /// <summary>
        /// 排序字段 ,如果有多个用,分隔
        /// </summary>
        public virtual string OrderBy { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage => (int)Math.Ceiling((decimal)Total / (decimal)PageSize);

        /// <summary>
        /// 升序或降序排序
        /// </summary>
        [AscDesc]
        public string AscDesc { get; set; }

        /// <summary>
        /// 关键字查询
        /// </summary>
        public string Query { get; set; }
    }

    public class ApiPageResult : PageBase
    {
        /// <summary>
        /// 当前页数据
        /// </summary>
        public object PageData { get; set; } = new object[] { };
    }

    public class ApiDataResult<T>
    {
        public ApiDataResult(T cdata)
        {
            data = cdata;
        }

        public T data { get; set; }
    }

    public class PageRequest
    {
        /// <summary>
        /// 页码，1开始
        /// </summary>
        [Range(1, 10000, ErrorMessage = "页码需要从1开始")]
        public int Page { set; get; } = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        [Range(1, 1000, ErrorMessage = "每页数量应该大于1")]
        public int PageSize { set; get; } = 10;

        /// <summary>
        /// 搜索关键词，模糊匹配
        /// </summary>
        public string Keywords { set; get; }

        /// <summary>
        /// 是否升序
        /// </summary>
        public bool IsAscend { get; set; }
    }
}